from _md5 import md5

import requests

from lib.sys.sys_config import SystemConfig
from login_server.template.template_verify_code import VerifyCode


class VertifyCodeUtils:

    code_type = "codetype"
    user_file = "userfile"
    id = "id"

    @staticmethod
    def analysis(im, codetype):
        params = {VertifyCodeUtils.code_type: codetype, }
        # verify_data = []
        verify_data = SystemConfig.get_verify_data()
        password = md5(verify_data[1].encode('utf8')).hexdigest()
        base_params = {'user': verify_data[0], 'pass2': password, 'softid': verify_data[2]}
        params.update(base_params)
        files = {VertifyCodeUtils.user_file: (VerifyCode.image_name, im)}
        r = requests.post(VerifyCode.url, data=params, files=files, headers=VerifyCode.headers)
        return r.json()

    @staticmethod
    def error(id):
        params = {VertifyCodeUtils.id: id, }
        params.update(VerifyCode.base_params)
        r = requests.post(VerifyCode.error_url, data=params, headers=VerifyCode.headers)
        return r.json()

